#include "registers_handler.h"

int isRegister(char * argument) {
	int regNum;
	
	/* checking if argument is in register format */
	if (argument[0] != '$' ||
		!isdigit(argument[1]) ||
	    (argument[2] != '\0' && !isdigit(argument[2]))) return -1;
	
	regNum = atoi(strtok(argument,"$"));
	
	/* checking if the register is in the valid range */
	if (regNum > (REGISTER_NUM-1)) return -1;
	
	return regNum;
}

int printRegisters(FILE * target) {
	int i,j;

	if (target == NULL) return -1;

	for (i = 0 ; i < REGISTER_NUM ; i++)
		fprintf(target,"$%d %d\n",i,registers[i]);

	return 0;
	
}

int printRegistersForTrace(FILE * target) {
	int i,j;

	if (target == NULL) return -1;

	j = 0;
	for (i = 0 ; i < REGISTER_NUM ; i++) {
		fprintf(target,"r%.2d: %.8X  ",i,registers[i]);
		j++;
		if (j == 8) {
			j = 0;
			fprintf(target,"\n");
		}
	}

	return 0;
	
}